package com.gdones.wms.bus.outbound.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.wms.bus.outbound.dto.OutBoundDto;
import com.gdones.wms.bus.outbound.dto.WmsOutbound;
import com.gdones.wms.bus.outbound.dto.WmsOutboundInfo;
import com.gdones.wms.bus.outbound.mapper.WmsOutboundInfoMapper;
import com.gdones.wms.bus.outbound.mapper.WmsOutboundMapper;
import com.gdones.wms.bus.outbound.service.IWmsOutboundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.wms.bus.record.dto.WmsRecord;
import com.gdones.wms.bus.record.dto.WmsRecordLife;
import com.gdones.wms.bus.record.mapper.WmsRecordLifeMapper;
import com.gdones.wms.bus.record.mapper.WmsRecordMapper;
import com.gdones.wms.core.util.MySysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 仓储出库记录主表 服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-08
 */
@Service
public class WmsOutboundServiceImpl extends ServiceImpl<WmsOutboundMapper, WmsOutbound> implements IWmsOutboundService {

    @Autowired
    private WmsOutboundInfoMapper outboundInfoMapper;

    @Autowired
    private WmsRecordMapper recordMapper;

    @Autowired
    private WmsRecordLifeMapper lifeMapper;

    @Override
    public MyResult pages(WmsOutbound dto, MyPageBand pageBand) {

        MyResult result = new MyResult();

        WmsOutboundMapper wmsOutboundMapper = getBaseMapper();

        IPage<WmsOutbound> pages = wmsOutboundMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<WmsOutbound> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setData(pager);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult list(WmsOutbound dto) {

        MyResult result = new MyResult();

        WmsOutboundMapper outboundMapper = getBaseMapper();

        List<WmsOutbound> list = outboundMapper.myList(dto);

        result.setMsg("查询成功！");
        result.setData(list);

        return result;
    }

    @Override
    public MyResult add(OutBoundDto dto) {

        MyResult result = new MyResult();

        WmsOutboundMapper outboundMapper = getBaseMapper();

        // 出库总量
        int outCount = 0;

        // 要出库的物资
        List<WmsOutboundInfo> infoList = dto.getInfoList();

        // 准备出库单
        WmsOutbound outbound = dto.getOutbound();
        outbound.setOutboundID(IdUtil.fastSimpleUUID());
        outbound.setCode("WMOUT-M-"+ MyPinYinUtil.getPinYinByString(outbound.getWarehouseName())+"-"+new Date().getTime());

        outbound.setOutboundTime(new Date());
        outbound.setRecordTypeCount(infoList.size());

        // 修改出库明细
        for(WmsOutboundInfo info : infoList){
            info.setOutboundID(outbound.getOutboundID());
            outCount += info.getOutCount();
            outboundInfoMapper.insert(info);
        }

        // 出库单保存
        outbound.setOutCount(outCount);
        outbound.setName(outbound.getWarehouseName()+"出库单-"+infoList.size()+"类物资-"+(new Date().getTime()));
        outboundMapper.insert(outbound);

        // 修改物资信息
        for(WmsOutboundInfo info : infoList){
            WmsRecord wmsRecord = recordMapper.selectById(info.getRecordID());
            wmsRecord.setWmsCount(wmsRecord.getWmsCount() - info.getOutCount());
            recordMapper.updateById(wmsRecord);

            // 每个物资生成出库记录
            // 准备记录信息
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            WmsRecordLife life = new WmsRecordLife();
            life.setRecordID(info.getRecordID());
            life.setRecordName(info.getRecordName());
            life.setLifeType("物资入库");
            life.setLifeTime(new Date());
            life.setLifeDesc(info.getRecordName()+"出库数量：【"+info.getOutCount()+"】，出库时间："+(sdf.format(new Date()))+"，出库人："+outbound.getUserName());
            lifeMapper.insert(life);
        }

        result.setMsg("出库成功！");

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] split = ids.split(",");

        List<String> list = Arrays.asList(split);

        WmsOutboundMapper outboundMapper = getBaseMapper();

        int i = outboundMapper.deleteBatchIds(list);

        if(i>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult getByID(String id) {

        MyResult result = new MyResult();

        OutBoundDto dto = new OutBoundDto();

        WmsOutboundMapper outboundMapper = getBaseMapper();

        WmsOutbound outbound = outboundMapper.selectById(id);

        // 查询出库明细
        QueryWrapper<WmsOutboundInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("outboundID",id);

        List<WmsOutboundInfo> wmsOutboundInfos = outboundInfoMapper.selectList(queryWrapper);

        dto.setOutbound(outbound);
        dto.setInfoList(wmsOutboundInfos);

        result.setData(dto);
        result.setMsg("查询成功！");

        return result;
    }
}
